package com.zdxf.service.impl;

import com.zdxf.domain.UserDO;
import com.zdxf.domain.UserTokenDO;
import com.zdxf.mapper.LoginMapper;
import com.zdxf.service.LoginService;
import com.zdxf.util.JwtTokenUtil;
import com.zdxf.util.Result;
import com.zdxf.util.UtilFun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    LoginMapper loginMapper;

    @Autowired
    @Qualifier("jwtUserDetailsService")
    private UserDetailsService userDetailsService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public Result login(Map params) {
        UserDO res = loginMapper.login(params);
        UserDO lockStatus = loginMapper.getLockStatus(params);
        int lockNum = lockStatus != null ? lockStatus.getError_num() : -1;
        Date date = new Date();
        String time = UtilFun.formatTime("yyyy年MM月dd日").format(date);
        System.out.println("--------⏰⏰⏰⏰⏰----------");
        System.out.println(lockNum);
        System.out.println("--------⏰⏰⏰⏰⏰----------");
        if (lockStatus != null && lockStatus.getLock_status() == 1) {
            if (lockStatus.getLock_time() == null) {
                System.out.println("--------⏰⏰⏰⏰⏰----------");
                System.out.println("锁定时间为空，设置锁定时间");
                System.out.println("--------⏰⏰⏰⏰⏰----------");
                loginMapper.setLockTime(params);
            } else {
                if (date.getTime() > lockStatus.getLock_time().getTime()) {
                    System.out.println("-------🔑🔑🔑🔑🔑-----------");
                    System.out.println("锁定时间失效，清空锁定状态");
                    System.out.println("-------🔑🔑🔑🔑🔑-----------");
                    loginMapper.resetLockStatus(params);
                    UserDO freshmen = loginMapper.login(params);
                    return allResult(params, freshmen, lockNum, time);
                } else {
                    System.out.println("-------🔓🔓🔓🔓🔓-----------");
                    System.out.println("锁定状态中");
                    System.out.println("-------🔓🔓🔓🔓🔓-----------");
                }
            }

            return new Result(false, 40001, "失败", "该账户验证次数已达上限，暂时冻结至" + time + "24点，请联系管理员进行修改");
        } else return allResult(params, res, lockNum, time);
    }

    private Result allResult(Map params, UserDO res, int locknum, String time) {


        if (res == null) {
            System.out.println("--------❌❌❌❌----------");
            System.out.println("用户名或密码错误");
            System.out.println("--------❌❌❌❌----------");
            loginMapper.setLockNum(params);
            String mes = "";
            if (locknum == -1) {
                mes = "没有此用户，请仔细核对账户后输入！";
            } else {
                if (locknum == 4) {
                    mes = "该账户验证次数已达上限，暂时冻结至" + time + "24点，请联系管理员进行修改";
                } else {
                    mes = "账号或密码错误，还有" + (4 - locknum) + "次机会，请仔细核对账户密码后输入！";
                }
            }

            return new Result(false, 40001, "失败", mes);
        } else {
            System.out.println("-------✅✅✅✅✅-----------");
            System.out.println("用户名或密码");
            System.out.println("-------✅✅✅✅✅-----------");
            final UserDetails userDetails = userDetailsService.loadUserByUsername((String) params.get("username"));
            final String token = jwtTokenUtil.generateToken(userDetails, false);
            final String refreshToken = jwtTokenUtil.generateToken(userDetails, true);
            System.out.println("-------token----------");
            System.out.println(token);
            System.out.println("--------token---------");
            res.setToken(token);
            res.setRefreshToken(refreshToken);
            loginMapper.resetLockStatus(params);
            loginMapper.delToken();
            return new Result(true, 20000, "成功", res);
        }
    }

    @Override
    public Result refresh(Map params) {
        final UserDetails userDetails = userDetailsService.loadUserByUsername((String) params.get("username"));
        final String token = jwtTokenUtil.generateToken(userDetails, false);
        final String refreshToken = jwtTokenUtil.generateToken(userDetails, true);
        UserDO res = new UserDO();
        res.setToken(token);
        res.setRefreshToken(refreshToken);
        return new Result(true, 20000, "刷新token成功", res);
    }

    @Override
    public Result logout(Map params) {
        loginMapper.setToken(params);
        return new Result(true, 20000, "成功", "退出登录成功");
    }

    @Override
    public UserTokenDO getTokenStatus(Map params) {
        return loginMapper.getTokenStatus(params);
    }
}
